package me.shurufa.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

import me.shurufa.R;
import me.shurufa.holder.CatalogNoNumViewHolder;
import me.shurufa.holder.MyDigestBookViewHolder;
import me.shurufa.model.Catalog;

/**
 *
 * 我的书摘本列表适配器
 *
 */
public class MyDigestBookAdapter extends RecyclerView.Adapter<MyDigestBookViewHolder> {

    List<Catalog> dataList;
    Object parent;

    public MyDigestBookAdapter(Object parent, List<Catalog> dataList) {
        this.dataList = dataList;
        this.parent = parent;
    }

    @Override
    public MyDigestBookViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.list_item_book_catalog, parent, false);
        MyDigestBookViewHolder holder = new MyDigestBookViewHolder(v, parent);
        return holder;
    }

    @Override
    public void onBindViewHolder(MyDigestBookViewHolder holder, int position) {
        Catalog catalog = dataList.get(position);
        holder.bind(catalog);
    }

    public void append(Catalog[] items) {
        int pos = dataList.size();
        for (Catalog item : items) {
            if (!dataList.contains(item))
                dataList.add(item);
        }
        notifyItemRangeInserted(pos, items.length);
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    public void remove(int position) {
        if (dataList.size() > 0) {
            dataList.remove(position);
            this.notifyItemRemoved(position);
        }
    }

    public void clear() {
        int size = dataList.size();
        dataList.clear();
        this.notifyItemRangeRemoved(0, size);
    }
}
